package com.pay.utils.http;

import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;


/**
 * Httpclient  连接池
 *
 * @author MENG
 * @version 2018/7/5
 * @see
 */
public class HttpClientPool
{
    private static PoolingHttpClientConnectionManager cm = null;

    static
    {
        cm = new PoolingHttpClientConnectionManager();

        cm.setMaxTotal(400);//池中最大连接数

        //连接某一个服务器的最大连接数   只有 请求1 个服务器所以 maxtotal = defaultMaxPerRoute
        cm.setDefaultMaxPerRoute(cm.getMaxTotal());

    }

    //获取http客户端
    public static CloseableHttpClient getHttpClient()
    {
        RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();

        CloseableHttpClient client = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(globalConfig).build();

        return client;
    }
}
